package com.wxh.bomb.netty.thread.thread.file;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
	static ExecutorService ex = Executors.newFixedThreadPool(4);

	public static void main(String[] args) {
		// 建立文件
		String pathname = "D:\\1111";
		File dir = new File(pathname);
		File[] files = null;
		if(dir.isDirectory()){
			files = dir.listFiles();
		} else {
			files = new File[]{dir};
		}
		MyFile[] myfiles = new MyFile[files.length];
		for(int i = 0; i < files.length; i++){
			myfiles[i] = new MyFile(files[i], i);
		}
		
		WriteThread[] wts = new WriteThread[files.length];
		for(int i = 0; i<files.length; i++){
			WriteThread wt = new WriteThread(i, myfiles);
			wts[i] = wt;
		}
		for(WriteThread wt : wts){
			ex.execute(wt);
		}
		while (true) {
			boolean allFinshed = true;
			for(WriteThread wt : wts){
				if(!wt.isFinished()){
					allFinshed = false;
					break;
				}
			}
			
			if (allFinshed) {
				ex.shutdown();
				new ReadThread(myfiles).print();
				break;
			}
		}
	}
}
